package com.yaorange.jk.web.cargo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.yaorange.jk.entity.Export;
import com.yaorange.jk.entity.PackingList;
import com.yaorange.jk.service.ExportService;
import com.yaorange.jk.service.PackingListService;
import com.yaorange.jk.utils.Pagination;
import com.yaorange.jk.web.BaseAction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.List;


@Controller("packingListAction")
@Scope("prototype")
public class PackingListAction extends BaseAction implements ModelDriven<PackingList>  {

    @Autowired
    private PackingListService packingListService;
    
    @Autowired
    private ExportService exportService;

    private Pagination page=new Pagination();

    private List<PackingList> packingListList;

    private PackingList model= new PackingList();

    public String list(){

    Pagination pagination =  packingListService.findPage(page);
    pagination.setUrl("packingListAction_list");
    ActionContext.getContext().getValueStack().push(pagination);
    return "tolist";
    }

    public String tocreate(){
    packingListList = packingListService.findAll();
    //ActionContext.getContext().getValueStack().set("packingListList",packingListList);
    page = exportService.findPage(page.getPageNo(), page.getPageSize(),1);
    page.setUrl("packingListAction_tocreate");
    push(page);
    return "tocreate";
    }

    public String insert(){
        packingListService.save(model);
        return list();
    }

    public String toupdate(){
    PackingList packingList = packingListService.findById(model.getId());
    super.push(packingList);

    packingListList = packingListService.findAll();
    packingListList.remove(packingList);
    return "toupdate";
    }

    public String update(){
    packingListService.update(model);
    return list();
    }
    
    public String cancel(){
    	String[] ids = model.getId().split(", ");
        for (String id:ids) {
        	
        	PackingList packingList = packingListService.findById(id);
        	packingListService.cancel(packingList);
        	String[] exportIds=packingList.getExportIds().split(", ");
        	for (String string : exportIds) {
				
        		Export export=exportService.findById(string);
        		export.setState(1); //设置export的状态为重置为1，上报
        		exportService.update(export);
			}
        }
    	return list();
    }
    
    public String topacking(){
    	String[] ids = model.getId().split(", ");
        for (String id:ids) {
        	
        	PackingList packingList = packingListService.findById(id);
        	packingListService.topacking(packingList);
        	String[] exportIds=packingList.getExportIds().split(", ");
        	for (String string : exportIds) {
				
        		Export export=exportService.findById(string);
        		export.setState(2); //设置export的状态为2，装箱
        		exportService.update(export);
			}
        }
        
    	return list();
    } 
    /**
    * @return
    */
    public String delete(){
    String[] ids = model.getId().split(", ");
    for (String id:ids) {
    packingListService.deleteById(model.getId());
    }

    return list();
    }

    public String toview()
    {
    PackingList packingList = packingListService.findById(model.getId());
    push(packingList);
    return "toview";
    }
    public void setPage(Pagination page) {
    this.page = page;
    }

    public List<PackingList> getPackingListList() {
    return packingListList;
    }

    @Override
    public PackingList getModel() {
    return model;
    }
}
